# Top Makefile
# 
# MIT License
# 
# Copyright(c) 2024-present All contributors of ebraid
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

TARGET    = $(CONFIG_CHIP)
CFLAGS    := $(MC_FLAG) $(C_PATH) $(C_DEFINE) $(C_FLAG)
ASFLAGS   := $(MC_FLAG) $(C_PATH) $(C_DEFINE) $(AS_FLAG)
LDFLAGS   := $(MC_FLAG) $(LD_FLAG) -T$(LDS_SOURCE).fina -Wl,-Map=$(BUILD_DIR)/$(TARGET).map


.PHONY: all


all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin elf_info


# list of c program objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCE:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCE)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCE:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCE)))


$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	@echo "CC   $<"
	@$(CC) -c $(CFLAGS) -MMD -MP \
		-MF  $(BUILD_DIR)/$(notdir $(<:.c=.d)) \
		-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
	@echo "AS   $<"
	@$(AS) -c $(ASFLAGS) -MMD -MP  \
		-MF $(BUILD_DIR)/$(notdir $(<:.S=.d)) $< -o $@


$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	@echo "LD   $@"
	@$(CC) -E -P -x assembler-with-cpp -include arch/$(CONFIG_ARCH)/linkpatch.h -include include/config.h $(LDS_SOURCE) -o $(LDS_SOURCE).fina
	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	@$(OD) $(BUILD_DIR)/$(TARGET).elf -xS > $(BUILD_DIR)/$(TARGET).s $@
	@echo ''
	@echo  "Build Successful!"
	@echo ''
	@echo "ELF   $@"
	@$(LM) --rm $(LDS_SOURCE).fina


$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	@echo "HEX   $@"
	@$(HEX) $< $@


$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	@echo "BIN   $@"
	@$(BIN) $< $@


elf_info: $(BUILD_DIR)/$(TARGET).elf
	@echo "=================================================================="
	@$(SZ) $<
	@echo "=================================================================="


flash: $(BUILD_DIR)/$(TARGET).bin
	@echo "FLASH: Start programming the $(BUILD_DIR)/$(TARGET).bin firmware into the target chip"
	@make -f tools/$(CONFIG_DEBUG_TOOL)/$(CONFIG_SERIES).mk  BIN=$< INTERFACE=$(CONFIG_DEBUG_INTERFACE)


uf2: $(ELF2UF2) $(BUILD_DIR)/$(TARGET).elf
	@echo "UF2: $(BUILD_DIR)/$(TARGET).uf2"
	@make -f tools/elf2uf2/$(CONFIG_SERIES).mk  CHIP=$(BUILD_DIR)/$(TARGET)
